<?php
namespace app\admin\validate;

/**
 * 验证器
 */
use think\Validate;

class GameCenter extends Validate
{
    protected $rule = [
        'version' => 'require|max:11|number|checkversion:thinkphp',
        'notice'  => 'require',
    ];

    protected $message = [
        'version.require'      => '公告版本号不能为空',
        'version.max'          => '公告版本号最多不能超过11个字符',
        'version.number'       => '公告版本号只能为数字',
        'version.checkversion' => '公告版本号已经存在',

        'notice.require'       => '公告内容不能为空',

    ];


    // 自定义验证规则
    protected function checkversion($value, $rule, $data = [])
    {
        if (empty($data['nid'])) {
           
            if (CreateGameModel($data['id'], 'user')->table('notice_info')->where([['version', '=', $data['version']]])->count() > 0) {
               return false; //当前
            }else{
                return true;
            }
        } else {
            if ((CreateGameModel($data['id'], 'user')->table('notice_info')->where([['version', '=', $data['version']]])->count() > 0)) {
                if ((CreateGameModel($data['id'], 'user')->table('notice_info')->where([['version', '=', $data['version']], ['id', '=', $data['nid']]])->count() > 0)) {
                    return true; //当前
                }else{
                    return false;
                }
            } else {
                return true;
            }
            
        }
    }
}
